/*
 * @Description: 
 * @Author: your name
 * @version: 
 * @Date: 2024-01-30 09:24:16
 * @LastEditors: your name
 * @LastEditTime: 2024-01-30 09:31:35
 */
import * as Cesium from 'cesium'

const waterAppearance = new Cesium.EllipsoidSurfaceAppearance({
    material: new Cesium.Material({
      fabric: {
        type: "self_water",
        uniforms: {

        },
        source:/*glsl*/`
        #define TAU 6.28318530718
        #define MAX_ITER 5

        czm_material czm_getMaterial(czm_materialInput materialInput) 
        {
            // 获取到最后返回的材质material
        czm_material material = czm_getDefaultMaterial(materialInput);
        // 获取到视口的宽高
        vec2 iResolution = czm_viewport.zw;
        // 获取到时间
        float iTime=czm_frameNumber/120.;
	    float time = iTime * .5+23.0;
        // uv should be the 0-1 uv of texture...
        // 获取到uv坐标
	    vec2 uv = materialInput.st;
    
        #ifdef SHOW_TILING
        	vec2 p = mod(uv*TAU*2.0, TAU)-250.0;
        #else
            vec2 p = mod(uv*TAU, TAU)-250.0;
        #endif
        	vec2 i = vec2(p);
        	float c = 1.0;
        	float inten = .005;
        
        	for (int n = 0; n < MAX_ITER; n++) 
        	{
        		float t = time * (1.0 - (3.5 / float(n+1)));
        		i = p + vec2(cos(t - i.x) + sin(t + i.y), sin(t - i.y) + cos(t + i.x));
        		c += 1.0/length(vec2(p.x / (sin(i.x+t)/inten),p.y / (cos(i.y+t)/inten)));
        	}
        	c /= float(MAX_ITER);
        	c = 1.17-pow(c, 1.4);
        	vec3 colour = vec3(pow(abs(c), 8.0));
            colour = clamp(colour + vec3(0.0, 0.35, 0.5), 0.0, 1.0);
            material.diffuse=colour;
            return material;
        }
        `
      },
    }),
    translucent:true
  })

export default waterAppearance